package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vos.AdUserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class AdUserServiceimpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        //参数校验
        if (dto == null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //根据用户名查找
        LambdaQueryWrapper<AdUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdUser::getName, dto.getName());
        AdUser user = this.getOne(queryWrapper);

        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_INEXISTENCE);
        }
        //9为账号正常可登录状态
        if (user.getStatus() != AdminConstants.AD_USER_ISSTATUS) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_STATUS_INVALID);
        }
        //判断密码是否正确
        String passWord = MD5Utils.encodeWithSalt(dto.getPassword(), user.getSalt());
        if (!passWord.equals(user.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //生成token
        String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
        //封装返回结果
        Map resultMap = new HashMap<>();
        AdUserVo userVo = new AdUserVo();
        BeanUtils.copyProperties(user, userVo);
        resultMap.put("user", userVo);
        resultMap.put("token", token);
        //7. 返回结果
        return ResponseResult.okResult(resultMap);

    }

    @Override
    public ResponseResult register(AdUser adUser) {
        //参数校验
        if (adUser == null || StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据用户名查询
        LambdaQueryWrapper<AdUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdUser::getName, adUser.getName());
        AdUser user = this.getOne(queryWrapper);

        //判断用户名是否已被占用
        if (user != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //生成一个随机的盐
        String salt = RandomStringUtils.randomAlphabetic(5);

        String password = MD5Utils.encodeWithSalt(adUser.getPassword(), salt);

        adUser.setSalt(salt);

        adUser.setPassword(password);

        adUser.setCreatedTime(new Date());

        adUser.setStatus(AdminConstants.AD_USER_ISSTATUS);
        //添加
        this.save(adUser);

        return ResponseResult.okResult();
    }
}
